package org.desktop.web.server.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class RpcUtil {

	public static HttpURLConnection getUrlConnection(URL _url,
			String requestMethod) throws IOException, ProtocolException {
		HttpURLConnection con = (HttpURLConnection) _url.openConnection();
		con.setAllowUserInteraction(true);
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setRequestMethod(requestMethod);
		return con;
	}

	public static void setToken(HttpURLConnection con,HttpServletRequest request) {
		String cookie = request.getHeader("Cookie");
		String[] cookiesList = StringUtils.split(cookie, ';');
		for (int i = 0; i < cookiesList.length; i++) {
			String string = cookiesList[i];
			String name = string.substring(0, string.indexOf("="));
			String value = string.substring(string.indexOf("=") + 1);
			System.out.println("-->" + name + "<--: " + "##" + value + "##");
			if (name.equals("token")) {
				con.setRequestProperty(name, value);
				break;
			}
		}
	}

}
